package com.hupu.selenium.jop.util;

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HttpClientUtils {
	
	private final static Logger LOGGER = LoggerFactory.getLogger(HttpClientUtils.class);
	
	private static final int TIME_OUT=1000*10;
	
	public static String httpGet(String url){
		CloseableHttpClient httpclient = HttpClients.createDefault();
		RequestConfig requestConfig = RequestConfig.custom()
		        .setSocketTimeout(TIME_OUT)
		        .setConnectTimeout(TIME_OUT)
		        .build();
		HttpGet httpGet = new HttpGet(url);
		httpGet.setConfig(requestConfig);;
		try {
			CloseableHttpResponse  response = httpclient.execute(httpGet);
			HttpEntity entity = response.getEntity();
			return EntityUtils.toString(entity);
		} catch (Exception e) {
			LOGGER.error("HTTP ERROR : "+url);
			e.printStackTrace();
		} finally{
			try {
				httpclient.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}
	
	public static void httpGetNotResult(String url){
		CloseableHttpClient httpclient = HttpClients.createDefault();
		RequestConfig requestConfig = RequestConfig.custom()
		        .setSocketTimeout(TIME_OUT)
		        .setConnectTimeout(TIME_OUT)
		        .build();
		HttpGet httpGet = new HttpGet(url);
		httpGet.setConfig(requestConfig);;
		try {
			httpclient.execute(httpGet);
		} catch (Exception e) {
			LOGGER.error(url);
			e.printStackTrace();
		} finally{
			try {
				httpclient.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
}
